
#ifndef __LWP_SIMPLE_H__
#define __LWP_SIMPLE_H__

#include "UserAgent.h"
#include "../HTTP.h"
#include "../HTTP/Connection.h"
#include "const.h"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

namespace LWP {

namespace Simple {

//--------------------------------------------------
//--------------------------------------------------

std::string get(const std::string& url)
{
	HTTP::RequestObject req(url, HTTP::HttpRequestType::GET_1_0);
	req.add_header("User-Agent", LWP::LWP_AGENT);
	HTTP::ResponseObject resp(req.host());
	HTTP::redirectable_request(req, resp);
	return resp.content();
}

std::vector<std::string> head(const std::string& url)
{
	HTTP::RequestObject req(url, HTTP::HttpRequestType::HEAD);
	req.add_header("User-Agent", LWP::LWP_AGENT);
	req.add_header("Connection", "close");
	HTTP::ResponseObject resp(req.host());
	HTTP::redirectable_request(req, resp);
	if(!resp.is_success())
		return std::vector<std::string>();
	std::vector<std::string> headers;
	headers.push_back(resp.get_header("Content-Type"));
	headers.push_back(resp.get_header("Content-Length"));
	headers.push_back(resp.get_header("Last-Modified"));
	headers.push_back(resp.get_header("Expires"));
	headers.push_back(resp.get_header("Server"));
	return headers;
}

std::string getprint(const std::string& url)
{
	LWP::UserAgent ua;
	HTTP::ResponseObject resp = ua.get(url);
	if(resp.is_success())
		std::cout<<resp.get_html();
	else
		std::cerr<<resp.get_error();
	return resp.status_code();
}

std::string getstore(const std::string& url, const std::string file)
{
	LWP::UserAgent ua;
	HTTP::ResponseObject resp = ua.get(url);
	if(resp.is_success())
	{
		std::ofstream out(file.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
		out<<resp.content();
	}
	return resp.status_code();
}

/*
std::string mirror(const std::string& url, const std::string& file)
{
}
*/
bool is_success(const std::string& code)
{
	return code.length()==3
	    && '1'<=code[0] && code[0]<='3'
	    && '0'<=code[1] && code[1]<='9'
	    && '0'<=code[2] && code[2]<='9';
}

bool is_error(const std::string& code)
{
	return !is_success(code);
}



//--------------------------------------------------
//--------------------------------------------------
namespace POST {

std::string post(const std::string& url, const std::string& data)
{
	HTTP::RequestObject req(url, HTTP::HttpRequestType::POST);
	req.add_header("User-Agent", LWP::LWP_AGENT);
	req.add_header("Connection", "close");
	req.content(data);
	HTTP::ResponseObject resp(req.host());
	redirectable_request(req, resp);
	return resp.content();
}

std::string post_xml(const std::string& url, const std::string& data)
{
	HTTP::RequestObject req(url, HTTP::HttpRequestType::POST);
	req.add_header("User-Agent", LWP::LWP_AGENT);
	req.add_header("Connection", "close");
	req.add_header("Content-Type", "text/xml");
	req.content(data);
	HTTP::ResponseObject resp(req.host());
	redirectable_request(req, resp);
	return resp.content();
}

}

//--------------------------------------------------
//--------------------------------------------------
/*
namespace Cookies {
}

*/

//--------------------------------------------------
//--------------------------------------------------

}//end namespace Simple
}//end namespace LWP

#endif

